package logistics;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;

import listening_function.show_data;
import listening_function.visit_data;

public class Repairs extends JPanel {
  private JTextField textField;
    private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField textField_6;
	private JTextField textField_7;
	private JTextField textField_8;
	private JTextField textField_9;
	private JTable table;

	/**
	 * Create the panel.
	 */
	public Repairs() {
		this.setSize(750, 410);
		setLayout(null);

		JPanel panel_2 = new JPanel();
		panel_2.setBounds(-1, 367, 751, 43);
		add(panel_2);
		panel_2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		//添加
		JButton btnNewButton = new JButton("添加");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				add1();
			}	
		});
		
		
		panel_2.add(btnNewButton);

		JButton btnNewButton_1 = new JButton("删除");
		btnNewButton_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				int[] row = table.getSelectedRows();
				for(int i = row[0] ; i<= row[row.length-1]; i++){
					String id=(String) table.getValueAt(i, 0);
				
					String sql = "delete from Repair where id='"+id+"'";
					new visit_data(sql);
					
				}
				newData();
			}
		});
		panel_2.add(btnNewButton_1);

		JButton btnNewButton_2 = new JButton("修改");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			 update();
				
				}
			
		});
		panel_2.add(btnNewButton_2);

		JButton btnNewButton_3 = new JButton("查询");
		panel_2.add(btnNewButton_3);

		JButton btnNewButton_4 = new JButton("刷新");
		btnNewButton_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				newData();
			}
		});
		panel_2.add(btnNewButton_4);

		JPanel panel = new JPanel();
		panel.setBounds(-1, 272, 751, 93);
		add(panel);
		panel.setLayout(new GridLayout(5, 4));

		JLabel lblNewLabel = new JLabel("\u7F16\u53F7:   ");
		lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
		panel.add(lblNewLabel);

		textField_1 = new JTextField();
		panel.add(textField_1);
		textField_1.setColumns(10);

		JLabel lblNewLabel_1 = new JLabel("\u62A5\u4FEE\u4EBA:   ");
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
		panel.add(lblNewLabel_1);

		textField = new JTextField();
		panel.add(textField);
		textField.setColumns(10);

		JLabel lblNewLabel_2 = new JLabel("\u62A5\u4FEE\u5730\u70B9:   ");
		lblNewLabel_2.setHorizontalAlignment(SwingConstants.RIGHT);
		panel.add(lblNewLabel_2);

		textField_2 = new JTextField();
		panel.add(textField_2);
		textField_2.setColumns(10);

		JLabel lblNewLabel_3 = new JLabel("\u62A5\u4FEE\u65F6\u95F4:   ");
		lblNewLabel_3.setHorizontalAlignment(SwingConstants.RIGHT);
		panel.add(lblNewLabel_3);

		textField_3 = new JTextField();
		panel.add(textField_3);
		textField_3.setColumns(10);

		JLabel lblNewLabel_4 = new JLabel("\u62A5\u4FEE\u7406\u7531:   ");
		lblNewLabel_4.setHorizontalAlignment(SwingConstants.RIGHT);
		panel.add(lblNewLabel_4);

		textField_4 = new JTextField();
		panel.add(textField_4);
		textField_4.setColumns(10);

		JLabel lblNewLabel_5 = new JLabel("\u7EF4\u4FEE\u72B6\u6001:   ");
		lblNewLabel_5.setHorizontalAlignment(SwingConstants.RIGHT);
		panel.add(lblNewLabel_5);

		textField_5 = new JTextField();
		panel.add(textField_5);
		textField_5.setColumns(10);

		JLabel lblNewLabel_6 = new JLabel("\u7EF4\u4FEE\u65F6\u95F4:   ");
		lblNewLabel_6.setHorizontalAlignment(SwingConstants.RIGHT);
		panel.add(lblNewLabel_6);

		textField_6 = new JTextField();
		panel.add(textField_6);
		textField_6.setColumns(10);

		JLabel lblNewLabel_7 = new JLabel("\u7EF4\u4FEE\u4EBA:   ");
		lblNewLabel_7.setHorizontalAlignment(SwingConstants.RIGHT);
		panel.add(lblNewLabel_7);

		textField_7 = new JTextField();
		panel.add(textField_7);
		textField_7.setColumns(10);

		JLabel lblNewLabel_8 = new JLabel("\u7EF4\u4FEE\u8D39\u7528:   ");
		lblNewLabel_8.setHorizontalAlignment(SwingConstants.RIGHT);
		panel.add(lblNewLabel_8);

		textField_8 = new JTextField();
		panel.add(textField_8);
		textField_8.setColumns(10);

		JLabel lblNewLabel_9 = new JLabel("\u62A5\u4FEE\u7269\u54C1:   ");
		lblNewLabel_9.setHorizontalAlignment(SwingConstants.RIGHT);
		panel.add(lblNewLabel_9);

		textField_9 = new JTextField();
		panel.add(textField_9);
		textField_9.setColumns(10);

		JPanel panel_1 = new JPanel();
		panel_1.setBounds(-1, 0, 751, 262);
		add(panel_1);
		panel_1.setLayout(null);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(0, 0, 751, 262);
		panel_1.add(scrollPane);

		table = new JTable();
		scrollPane.setViewportView(table);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				int rowIndex=table.getSelectedRow();//获取行坐标 
				int colIndex=table.getSelectedColumn();//获取列数 
				for(int i=0;i<colIndex;i++){
					textField_1.setText((String) table.getValueAt(rowIndex, 0));
					textField.setText((String) table.getValueAt(rowIndex, 1));
					textField_2.setText((String) table.getValueAt(rowIndex, 2));
					textField_3.setText((String) table.getValueAt(rowIndex, 3));
					textField_4.setText((String) table.getValueAt(rowIndex, 4));
					textField_5.setText((String) table.getValueAt(rowIndex, 5));
					textField_6.setText((String) table.getValueAt(rowIndex, 6));
					textField_7.setText((String) table.getValueAt(rowIndex, 7));
					textField_8.setText((String) table.getValueAt(rowIndex, 8));
					textField_9.setText((String) table.getValueAt(rowIndex, 9));
					}
			
				}
			
		});
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null},
			},
			new String[] {
				"\u5E8F\u53F7", "\u62A5\u4FEE\u4EBA", "\u62A5\u4FEE\u5730\u70B9", "\u62A5\u4FEE\u65F6\u95F4", "\u62A5\u4FEE\u7406\u7531", "\u7EF4\u4FEE\u72B6\u6001", "\u7EF4\u4FEE\u65F6\u95F4", "\u7EF4\u4FEE\u4EBA", "\u7EF4\u4FEE\u8D39\u7528", "\u62A5\u4FEE\u7269\u54C1"
			}
		));
     new show_data("Repair", table);///////
	}
	public void add1(){
		String id = textField_1.getText();
		String name = textField.getText();
		String where = textField_2.getText();
		String time = textField_3.getText();
		String reason = textField_4.getText();
		String goods = textField_9.getText();
		String sql = "insert into Repair values('"
				+id+"','"+name+"','"+where+"','"+time+"','"
				+reason+"','"+"0"+"','"+"0"+"','"+"0"+"','"+"0"+"','"+goods+"')";
		new visit_data(sql);
		//显示
		String [] a = {id,name,where,time,reason,goods};
		int count= visit_data.row_check("Repair")-1;
		for(int i = 0 ; i < 6; i++){
			if(i==5){
				table.setValueAt(goods, count, 9);
				table.setValueAt("null", count, 5);
				table.setValueAt("null", count, 6);
				table.setValueAt("null", count, 7);
				table.setValueAt("null", count, 8);
				break;
			}
			table.setValueAt(a[i], count, i);
		}
	}
	public void update(){
		String id = textField_1.getText();
		String name = textField.getText();
		String PLACE = textField_2.getText();
		String time = textField_3.getText();
		String reason = textField_4.getText();
		String STATE = textField_5.getText(); 
		String MAINTENANCETIME = textField_6.getText();
		String MAINTENANCEMAN = textField_7.getText();
		String COST= textField_8.getText();
		String REPAIRITEMS = textField_9.getText();
		String sql = "UPDATE repair SET id='"+id+"', name='"+name+"',PLACE='"+
				PLACE+"',time='"+time+"',reason='"+reason+"',STATE='"+STATE+"',MAINTENANCETIME='"+MAINTENANCETIME+
						"',MAINTENANCEMAN='"+MAINTENANCEMAN+"',COST='"+COST+"',REPAIRITEMS='"+REPAIRITEMS+"' WHERE id='"+id+"'";
		new visit_data(sql);
	}
	public void newData() {
		for(int i = 0 ; i < table.getRowCount();i++){
			for(int j = 0; j < table.getColumnCount();j++){
				table.setValueAt(null, i, j);
			}
		}
		new show_data("Repair",table);
	}
}